package com.krm.codemanager.service;

import java.beans.IntrospectionException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.krm.codemanager.mapper.CategoryMapper;
import com.krm.codemanager.model.CategoryMaintain;

import utils.CommonEntity;
import utils.ExcelBean;
import utils.ExcelUtils;

/**
 * @Title: ReportService.java
 * @Package com.krm.web.design.service
 * @Description: TODO(用一句话描述该文件做什么)
 * @author: yys
 * @date: 2018年6月11日 下午2:26:10
 * @version report V2.0
 */
@Service("categoryService")
public class CategoryService {

	@Resource
	private CategoryMapper categoryMapper;

	public PageInfo<utils.CommonEntity> queryPageInfo(Map<String, Object> params) {
		PageHelper.startPage(params);
		List<CommonEntity> list = null;
		list = categoryMapper.pageInfo(params);
		return new PageInfo<CommonEntity>(list);
	}

	public int addCategory(CategoryMaintain categoryMaintain) {
		return categoryMapper.addCategory(categoryMaintain);
	}

	public int updateCategory(CategoryMaintain categoryMaintain) {
		return categoryMapper.updateCategory(categoryMaintain);
	}

	public int delCategory(Integer categoryId) {
		return categoryMapper.delCategory(categoryId);
	}

	public int insertBatch(List<CategoryMaintain> list) {
		// TODO Auto-generated method stub
		return categoryMapper.insertBatch(list);
	}

	public List<CategoryMaintain> entityList(Map<String, Object> params) {
		List<CategoryMaintain> entityList = new ArrayList<>();
		// TODO Auto-generated method stub
		List<CommonEntity> list = categoryMapper.pageInfo(params);
		for (CommonEntity commonEntity : list) {
			entityList.add(JSON.toJavaObject(commonEntity, CategoryMaintain.class));
		}
		return entityList;
	}

	public List<CategoryMaintain> queryCategory(String createUser) {
		// TODO Auto-generated method stub
		return categoryMapper.queryCategory(createUser);
	}

	public CategoryMaintain selectById(Integer categoryId) {
		return categoryMapper.getCategoryByStdCatId(categoryId.toString());
	}

	public String uploadPayerCreditInfoExcel(InputStream in, MultipartFile file) throws Exception {
		// TODO Auto-generated method stub
		List<List<Object>> listob = ExcelUtils.getBankListByExcel(in, file.getOriginalFilename());
		//List<CategoryMaintain> creditInfoList = new ArrayList<CategoryMaintain>();
		int j = 0;
		int k = 0;
		for (int i = 0; i < listob.size(); i++) {
			List<Object> ob = listob.get(i);
			CategoryMaintain creditInfoBean = new CategoryMaintain();
			if( ob.get(0)!=null && !ob.get(0).equals("")){
				creditInfoBean.setStdCatType(String.valueOf(ob.get(0)));
			}
			if( ob.get(1)!=null && !ob.get(1).equals("")){
				creditInfoBean.setStdCatName(String.valueOf(ob.get(1)));
			}
			if( ob.get(2)!=null && !ob.get(2).equals("")){
				creditInfoBean.setCreateTime(String.valueOf(ob.get(2)));
			}
			if( ob.get(3)!=null && !ob.get(3).equals("")){
				creditInfoBean.setModefyTime(String.valueOf(ob.get(3)));
			}
			if( ob.get(4)!=null && !ob.get(4).equals("")){
				creditInfoBean.setCreatorName(String.valueOf(ob.get(4)));
			}
			if( ob.get(5)!=null && !ob.get(5).equals("")){
				creditInfoBean.setMaintainerName(String.valueOf(ob.get(5)));
			}
			CategoryMaintain onlyone = categoryMapper.getOnlyone(creditInfoBean);
			if(onlyone!=null){
				j=j+1;
			}else{
				categoryMapper.addCategory(creditInfoBean);
				k=k+1;
			}
			
			//creditInfoList.add(creditInfoBean);
		}
		String msg = "导入成功"+k+"条，重复数据"+j+"条";
		return msg;
		}
		

	

	public XSSFWorkbook exportPayerCreditInfoExcel(String user) {
		List<CategoryMaintain> creditInfoList = queryCategory(user);
		List<ExcelBean> ems = new ArrayList<>();
		Map<Integer, List<ExcelBean>> map = new LinkedHashMap<>();
		ems.add(new ExcelBean("代码类别", "stdCatType", 0));
		ems.add(new ExcelBean("类别名称", "stdCatName", 0));
		ems.add(new ExcelBean("创建时间", "createTime", 0));
		ems.add(new ExcelBean("修改时间", "modefyTime", 0));
		ems.add(new ExcelBean("创建人", "creatorName", 0));
		ems.add(new ExcelBean("维护人", "maintainerName", 0));
		map.put(0, ems);
		XSSFWorkbook book = null;
		try {
			book = ExcelUtils.createExcelFile(CategoryMaintain.class, creditInfoList, map, "类别维护");
		} catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException | ClassNotFoundException
				| IntrospectionException | ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return book;
	}

	public CategoryMaintain getOnlyone(CategoryMaintain categoryMaintain) {
		// TODO Auto-generated method stub
		return categoryMapper.getOnlyone(categoryMaintain);
	}
}
